import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_quick/Controller/im_controller.dart';
import 'package:flutter_quick/Interfaces/IM.dart';
import 'package:flutter_quick/components/contact-item.dart';
import 'package:flutter_quick/components/search.dart';
import 'package:get/get.dart';

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [More()],
        title: const Text(
          '微信',
          style: TextStyle(color: Colors.black, fontSize: 18),
        ),
        centerTitle: true,
        backgroundColor: const Color(0xffededed),
        elevation: 0,
      ),
      body: Conversation(),
    );
  }
}

class Conversation extends StatefulWidget {
  Conversation({Key? key}) : super(key: key);

  @override
  _ConversationState createState() => _ConversationState();
}

class _ConversationState extends State<Conversation>
    with AutomaticKeepAliveClientMixin {
  List<ConversationItem> _conversationList = [];

  @override
  void initState() {
    super.initState();
    getConversationList();
  }

  getConversationList() {
    IMController imController = Get.find<IMController>();

    Timer(
      const Duration(seconds: 1),
      () async {
        if (mounted) {
          List<ConversationItem> list =
              await imController.im.getConversationList();
          setState(() {
            _conversationList = list;
          });
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Container(
          padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
          color: Color(0xffededed),
          child: Search(),
        ),
        Divider(
          height: 1,
          color: Color(0xffe2e2e2),
        ),
        ..._conversationList
            .map((e) => ContactItem(
                  conversationItem: e,
                ))
            .toList()
      ],
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

/// 更多
class More extends StatefulWidget {
  More({Key? key}) : super(key: key);

  @override
  _MoreState createState() => _MoreState();
}

class _MoreState extends State<More> {
  List<PopupMenu> popupMenus = [
    PopupMenu(text: '发起群聊', icon: Icons.chat_bubble),
    PopupMenu(text: '添加朋友', icon: Icons.person_add_alt_1),
    PopupMenu(text: '扫一扫', icon: Icons.search),
    PopupMenu(text: '收付款', icon: Icons.qr_code_scanner_outlined),
  ];

  @override
  Widget build(BuildContext context) {
    return PopupMenuButton(
      icon: const Icon(
        Icons.add_circle_outline_sharp,
        color: Colors.black,
      ),
      offset: const Offset(0, 52),
      color: const Color(0xff404040),
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
      onSelected: (result) {},
      itemBuilder: (BuildContext context) {
        return popupMenus
            .map(
              (item) => PopupMenuItem(
                padding: EdgeInsets.symmetric(horizontal: 24),
                height: 52,
                child: Row(
                  children: [
                    Icon(item.icon, size: 22),
                    SizedBox(
                      width: 9,
                    ),
                    Text(
                      item.text,
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ],
                ),
              ),
            )
            .toList();
      },
    );
  }
}

class PopupMenu {
  String text;
  IconData icon;

  PopupMenu({required this.text, required this.icon});
}
